package stone;

import java.util.ArrayList;

import stone.ast.ClassStmnt;
import stone.ast.DefStmnt;
import ASTInterpreter.Environment;

public class OptClassInfo extends ClassInfo {
	
	protected Symbols methods, fields;
	protected DefStmnt[] methodDefs;

	public OptClassInfo(ClassStmnt cs, Environment env, Symbols methods, Symbols fields) {
		super(cs, env);
		this.methods = methods;
		this.fields = fields;
		this.methodDefs = null;
	}
	
	public int size() {
		return fields.size();
	}
	
	@Override
	public OptClassInfo superClass() {
		return (OptClassInfo)superClass;
	}
	
	public void copyTo(Symbols f, Symbols m, ArrayList<DefStmnt> mlist) {
		// 将methods，fields以及methodDefs字段拷贝到参数中
		// 这个函数用于类定义解析时，将父类的这3个字段拷贝到子类当中
		// 参见: ClassStmnt.eval
		f.append(fields);
		m.append(methods);
		for (DefStmnt def : methodDefs) {
			mlist.add(def);
		}
	}
	
	public Integer fieldIndex(String name) {
		// 根据字段名查找该字段的下标
		return fields.find(name);
	}
	
	public Integer methodIndex(String name) {
		// 根据方法名查找方法的下标
		return methods.find(name);
	}
	
	public Object method(OptStoneObject self, int index) {
		DefStmnt def = methodDefs[index];
		return new OptMethod(def.parameters(), def.body(), environment(), def.locals(), self);
	}
	
	public void setMethods(ArrayList<DefStmnt> methods) {
		methodDefs = methods.toArray(new DefStmnt[methods.size()]);
	}

}
